a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

import itertools

ans = 0
s = 0

div = [2, 3, 5, 7, 11, 13, 17]

for pa in itertools.permutations(a):
    if pa[0] == 0:
        continue
    n = ''.join([str(x) for x in pa])
    good = 1
    for i in xrange(7):
        nn = int(n[i + 1:i+4])
        if nn % div[i] != 0:
            good = 0
            break
    if good:
        ans += 1
        s += int(n)
        print ans, n

print ans
print s
